/********************************************************************

Write Date: 2015/04/14
Author: Apple
>>>> Implement Class _ObjectCreateHelper.

Modify Date: 2015/08/13
Author: Apple
>>>> Add the ability to record the name of newing Objects.

*********************************************************************/
#pragma once
#include <QtCore>
#include <QSet>
#include <QMutex>
#include "Singleton.h"



_NS_OOP_BEGIN



/**
 * [Internal Class]
 * Class _ObjectCreateHelper is to help recognize whether an object is created
 * on the heap (by new operator) or not. When an object is created by
 * new operator, recordHeapCreate() is called. Then the constructor of 
 * class Object will call checkHeapCreate() to check whether there is a record.
 *
 * Thread-Safe: true.
 *
 * @remarks 2015/04/14, Apple
 */
class _ObjectCreateHelper : public Singleton< _ObjectCreateHelper >
{

	friend class Singleton< _ObjectCreateHelper >;

public:

	/**
	 * Record current thread is newing an Object and store the name of the 
	 * Object to create.
	 *
	 * @param r_name - the name of the Object to create
	 */
	void recordNewingObject (const QString& r_name);

	/**
	 * Record current thread has finished newing an Object.
	 */
	void recordNewingObjectFinished ();

	/**
	 * Check whether current thread is newing an Object.
	 *
	 * @return whether current thread is newing an Object
	 */
	bool isNewingObject () const;

	/**
	 * For current thread, check the object in creation is on the heap or not.
	 *
	 * @return whether there is a record for current thread
	 */
	QString nameOfNewingObject () const;

private:

	/* Threads newing Object and their related name specified in operator new */
	QMap< Qt::HANDLE, QString > _map_ObjsNewing;
	/* The mutex lock to prevent access conflicts to _map_ObjsNewing */
	mutable QMutex _mutex;

	/**
	 * The default constructor.
	 */
	_ObjectCreateHelper ();

};



_NS_OOP_END